1873H - Mad City - CodeForces Solution


dfs and similar graphs shortest paths

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pb_ds;
#define pb push_back
#define Y cout << "YES" << endl;
#define N cout << "NO" << endl;
#define ll long long
#define mod 1000000007

vector<ll> graph[200005];
int vis[200005];
vector<ll> pa(200005, 0);

vector<ll> cyc(200005, 0);
bool check = false;

void dfs(ll v, ll par)
{

    if (check)
    {
        return;
    }

    vis[v] = true;
    for (auto u : graph[v])
    {
        if (check)
        {
            return;
        }
        if (!vis[u])
        {

            pa[u] = v;
            dfs(u, v);
        }
        else if (u != par)
        {
            check = true;
            ll node = v;

            while (node != u)
            {
                cyc[node] = 0;
                node = pa[node];
            }
            cyc[u] = 0;

            return;
        }
    }

    if (check)
    {
        return;
    }
}

ll cnt = 0;

ll mx = INT_MAX;

ll nd = -1;

void dfs1(ll v)
{
    vis[v] = true;

    for (auto u : graph[v])
    {
        if (!vis[u])
        {
            cnt++;

            if (cyc[u] == 0)
            {

                if (mx > cnt)
                {
                    mx = cnt;
                    nd = u; // is node pe entry marega
                }
            }
            dfs1(u);
            cnt--;
        }
    }
}
void solve()
{
    ll n, a, b;
    cin >> n >> a >> b;
    a--;
    b--;
    check = false;
    
    for (ll i = 0; i < n; i++)
    {
        graph[i].clear();
        vis[i] = false;
        cyc[i] = INT_MAX;
    }

    for (ll i = 0; i < n; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    dfs(0, -1);

    // for(ll i=0;i<n;i++){
    // cout<<cyc[i]<<" ";
    // }
    // cout<<endl;
    if (a == b)
    {
        N return;
    }
    // agar pehle se cycle mein ho loda kuch nahi hoga
    if (cyc[b] == 0)
    {
        Y return;
    }
    // wo jaldi pahuchna chahta hai cycle ke andar
    mx = INT_MAX;
    nd = -1;
    cnt = 0;
    memset(vis, false, sizeof(vis));

    dfs1(b);

    memset(vis, false, sizeof(vis));

    queue<ll> q;
    q.push(a);
    vector<ll> dis(n, -1);
    dis[a] = 0;
    while (!q.empty())
    {
        ll node = q.front();
        q.pop();
        for (auto u : graph[node])
        {
            if (dis[u] == -1)
            {
                q.push(u);
                dis[u] = dis[node] + 1;
            }
        }
    }

    ll ds = dis[nd];

    if (ds > mx)
    {
        Y return;
    }
    N
}
int main()
{
    ll test;
    cin >> test;
    while (test--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction